<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <base href="<%=basePath%>">
    <title>Title</title>
    <%@include file="../common_css.jsp"%>
</head>
<body>
<script id="toolbar" type="text/html">
    <form action=" " class="layui-form" layui-fluid>
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="name"
                           class="layui-input
					searchVal" placeholder="活动名称" />
                </div>
                <a class="layui-btn search_btn" data-type="reload" onclick="setCondition()"><i
                        class="layui-icon">&#xe615;</i> 搜索</a>
            </div>
        </form>

    </form>

    <br/>
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-normal addNews_btn" lay-event="add" type="button" onclick="Toadd()">
            <i class="layui-icon">&#xe608;</i>
            添加活动
        </a>
    </div>
</script>
<!--操作-->
<script id="userListBar" type="text/html">
    <a class="layui-btn layui-btn-xs"  type="button" onclick="Toedit({{d.id}})">编辑</a>
</script>
    <table id="tbl"></table>
<%--    <script src="layuiadmin/layui/layui.js"></script>--%>
    <script>
        var table;
        var jquery;
        layui.config({
            base:'layuiadmin/',
        }).extend({
            index:'lib/index'
        }).use(['table','jquery'],function () {
            table=layui.table;
            jquery=layui.jquery;
            table.render({
                id:"tbl",
                elem:"#tbl",
                url:"activity/index",
                method:"post",
                page:"true",
                toolbar:"#toolbar",
                contentType:"application/x-www-form-urlencoded",
                limit:4,
                parseData:function (res) {
                        return{
                            code:0,
                            msg:"",
                            data:res.data,
                            count:res.count,
                        }
                },
                cols:[[

                    {type:'checkbox'},
                    {
                        field:"id",
                        title:"编号",
                    },
                    {
                        field:"name",
                        title:"活动名称 ",
                    },
                    {
                        field:"place",
                        title:"地点",
                    },
                    {
                        field:"startTime",
                        title:"开始时间",
                    },
                    {
                        field:"endTime",
                        title:"结束时间",
                    },
                    {
                        field:"createTime",
                        title:"创建时间",
                    },
                    {
                        field:"updateTime",
                        title:"修改时间",
                    },
                    {
                        title: "操作",
                        templet:"#userListBar",
                    }



                ]]
            })
        })

        function setCondition() {
            table.reload('tbl',{
                where:{
                    "name":jquery('[name=name]').val(),
                },
            })

        }
        function Toadd() {
            var index=layer.open({
                type: 2,
                title:"新增活动",
                content:"activity/add",
                btn:['添加','取消'],
                area: ['700px', '400px'],//弹窗的长宽 默认auto时 maxWidth - 最大宽度 maxHeight - 最大高度3
                shadeClose:true,
                yes:function (index,layero) {
                    var iframe=jquery(layero).find('iframe')[0];
                    var $form=jquery(iframe.contentWindow.document).find('form');
                    jquery.post($form.attr('action'),$form.serialize(),function (res) {
                        table.reload('tbl',{
                            page: {
                                curr:1,
                            }
                        });
                        layer.close(index);

                    });


                },
                btn2:function (index,layero) {

                }
            })

        }


        function Toedit(id) {
            var index=layer.open({
                type: 2,
                title:"修改活动",
                content:"activity/update/"+id,
                btn:['修改','取消'],
                area: ['700px', '400px'],//弹窗的长宽 默认auto时 maxWidth - 最大宽度 maxHeight - 最大高度3
                yes:function (index,layero) {
                    var iframe=jquery(layero).find('iframe')[0];
                    var $form=jquery(iframe.contentWindow.document).find('form');
                    jquery.post($form.attr('action'),$form.serialize(),function (res) {
                        table.reload('tbl',{
                            page: {
                                curr:1,
                            }
                        });
                        layer.close(index);

                    });


                },
                btn2:function (index,layero) {

                }
            })

        }

        function Todel() {
            var layer;
            //yin
            layui.use('layer',function () {
                layer=layui.layer;
            })
            //获取选中行
            var checkStatus= table.checkStatus('tbl');
            //至少有一行呗选中 才能删除数据
            if(checkStatus.data.length==0){
                layer.msg("至少选中一行来删除");
            }

            //从选中行中获取
            var params={};
            for (var index in checkStatus.data){
                params['ids['+index+']']= checkStatus.data[index].id;
            }
            console.log(params);

            jquery.ajax({
                url: "activity/del",
                method: "post",
                contentType:"application/x-www-form-urlencoded",
                data:params,
                success:function (res) {
                    if(res.msg==="success"){
                        table.reload('tbl');
                    }
                }
            })

        }

        function Todelete(id) {
            //提示是否确认删除
            var index=  layer.confirm('是否确认删除', {
                btn: ['确认', '取消'] //可以无限个按钮
            }, function(index, layero){
                alert("确定删除")
                //ajax
                jquery.post('activity/delete/'+id,function (res) {
                    if(res.msg){
                        //重新加载
                        table.reload('tbl',{
                            page:{
                                curr:1
                            }
                        });
                        layer.close(index);
                    }else{
                        layer.msg("删除失败");
                    }
                });
            }, function(index){
                alert("取消删除")
            });

        }

    </script>

</body>
</html>
